import { history } from "../../services/diagnosis"
import { getSexName, formatDateToZ
  // , countResult 
} from "../../utils/util"
Page({
  data: {
    users_id: null,
  },
  onPullDownRefresh() {
    this.queryHistory(true)
  },
  onReachBottom() {    
    this.queryHistory()
  },
  queryHistory(isRefresh = false) {
    if(isRefresh) {
      this.setData({
        userList: [],
        page: 1,
        hasMore: true
      })
    } else {
      if(!this.data.hasMore) return 
      this.data.page = this.data.page + 1
    }
    history((data)=> {
      let userList =  [...this.data.userList, ...data.list]
      console.log(userList)
      this.setData({
        userList: userList,
        hasMore: this.data.page < data.all_page
      })
      wx.stopPullDownRefresh();
    }, this.data.page)
  },
  onLoad(options) {
    this.queryHistory(true)
  },
  onDiagnosisResultTap(e) {
    const {
      username,
      sex,
      birth_solar,
      hand_pic,
      created_at
    } = this.data.userList[e.currentTarget.dataset.item]
    let birthArr = birth_solar.split("-")
    console.log(birthArr)
    // let reportContent = index + ". 您的先天弱脏为" + countResult(birthArr[0],birthArr[1],birthArr[2]).replaceAll("、","") 
    const resultList = hand_pic.map(item => item.infer_result);
    
    let index = 1
    let reportContent = ""
    for (let i in resultList) {
      // if(!resultList[i]) continue
      reportContent += "#+#reportContent_" + (index++) + "#-#" + resultList[i]
    }
    console.log(reportContent)
    wx.navigateTo({
      url: `/pages/diagnosis-result/diagnosis-result?username=${username}&gender=${getSexName(sex)}&birthdate=${formatDateToZ(birth_solar)}&reportContent=${reportContent}&reportDate=${formatDateToZ(created_at.substring(0, 10))}`
    })
  },
  onImgViewTap(e) {
    let {item, imgindex} = e.currentTarget.dataset
    let imgList = this.data.userList[item].hand_pic.map(item => item.img)
    wx.previewImage({
      current: imgList[imgindex], // 当前点击的预览图片地址
      urls: imgList// 需要预览的图片列表，这里只有一张图片
    });
  }
})